package com.cch.cooperation.codegenerator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 *
 * @author cch
 */
@Slf4j
public class GeneratorStart {

    private static String subProjectName = "cooperation-dao";
    private static String module = "vip"; // 模块名称
    private static String[] tables = new String[]{"vip_ad_config"};
    private static String projectPath = System.getProperty("user.dir");

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator generator = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/" + subProjectName + "/src/main/java");
        gc.setAuthor("cch");
        gc.setOpen(false);
        generator.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.94.136.237:3306/cooperation");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("mysql@!QAZ2wsx");
        generator.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(module); // 模块名称
        pc.setParent("com.cch.cooperation.dao");
        pc.setEntity("model");
        generator.setPackageInfo(pc);
        // 自定义输出类型
        TemplateConfig template = new TemplateConfig();
        template.setEntity("templates/model.java");
        template.setController(null);
        template.setXml(null);
        template.setService(null);
        template.setServiceImpl(null);
        generator.setTemplate(template);
        // 自定义输出路径
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                this.setMap(map);
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/" + subProjectName + "/src/main/resources/mybatis/" + module.replace(".", "/") + "/" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        generator.setCfg(cfg);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(tables);
        strategy.setControllerMappingHyphenStyle(true);
        generator.setStrategy(strategy);
        generator.execute();
    }

}
